package main

import (
	"context"
	"fmt"
	"github.com/golang/protobuf/proto"
	"github.com/valyala/fasthttp"
	"google.golang.org/grpc/metadata"
	"kop/pb"
	"time"
)

func main() {

	var mess pb.RawItem
	mess.ItemID = 101001
	mess.Number = 1

	var msg, _ = proto.Marshal(&mess)

	var req fasthttp.Request
	req.SetRequestURI("http://127.0.0.1:10000/Item/AddItem")
	req.Header.Add("Token", "05c76b3f5847d9caae5c5855edb3ee31")
	req.SetBody(msg)
	req.Header.SetContentType("application/protobuf")
	req.Header.SetMethod("POST")

	var resp fasthttp.Response

	var err = fasthttp.Do(&req, &resp)

	var item pb.Item
	_ = proto.Unmarshal(resp.Body(), &item)

	fmt.Println(resp.Header.StatusCode(), item, err)
}

func ctx(token string) (context.Context, context.CancelFunc) {
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	ctx = metadata.NewOutgoingContext(ctx, metadata.New(map[string]string{"token": token}))
	return ctx, cancel
}
